package com.shd.edanpei.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.TextView;

import com.shd.edanpei.R;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.presenter.contract.EditContract;
import com.shd.edanpei.presenter.EditPresenter;
import com.shd.edanpei.base.network.ApiParams;
import com.shd.edanpei.view.ViewEditText;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * Created by mc on 2018/4/4.
 * <p>
 * 编辑
 */

public class EditActivity extends BaseActivity<EditPresenter> implements EditContract.EditIView {

    public static final String INTENT_RESULT_CODE = "result_code";
    public static final String INTENT_BACK_DATA = "back_data";
    public static final String TITLE = "title";
    public static final String CONTENT = "content";

    /**
     * 请求码
     */
    public static final int INTENT_REQUEST_CODE = 10;

    @BindView(R.id.headline)
    TextView headline;
    @BindView(R.id.tv_headline_right)
    TextView tvHeadlineRight;
    @BindView(R.id.et_edit)
    ViewEditText etEdit;

    /**
     * 标题
     */
    private String title;
    /**
     * 内容
     */
    private String content;

    /**
     * 结果码
     */
    private int mResultCode;

    @Override
    protected int setLayout() {
        return R.layout.activity_edit;
    }

    @Override
    protected EditPresenter bindPresenter() {
        return new EditPresenter(this);
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        title = getIntent().getStringExtra(TITLE);
        content = getIntent().getStringExtra(CONTENT);
        mResultCode = getIntent().getIntExtra(INTENT_RESULT_CODE, INTENT_REQUEST_CODE);
        headline.setText(title);
        tvHeadlineRight.setText("保存");
        tvHeadlineRight.setTextColor(ContextCompat.getColor(EditActivity.this, R.color.light_gray));
        tvHeadlineRight.setClickable(false);
        etEdit.setText(content);
        etEdit.setSelection(content.length());
    }

    @Override
    protected void initListener() {
        etEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //对比参数是否相同
                if (content.equals(s.toString()) || s.length() == 0) {
                    tvHeadlineRight.setTextColor(ContextCompat.getColor(EditActivity.this, R.color.light_gray));
                    tvHeadlineRight.setClickable(false);
                } else {
                    tvHeadlineRight.setTextColor(ContextCompat.getColor(EditActivity.this, R.color.main_color));
                    tvHeadlineRight.setClickable(true);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @OnClick({R.id.iv_headline_back, R.id.tv_headline_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_headline_back:
                finish();
                break;
            case R.id.tv_headline_right:
                mPresenter.update(ApiParams.NICK_NAME, etEdit.getText().toString());
                break;
        }
    }

    @Override
    public void updateSuccess(String data) {
        Intent intent = new Intent();
        intent.putExtra(INTENT_BACK_DATA, etEdit.getText().toString());
        setResult(mResultCode, intent);
        finish();
    }

    /**
     * 返回码
     *
     * @param activity
     * @param code
     */
    public static void start(Activity activity, int code, String title, String content) {
        Intent intent = new Intent(activity, EditActivity.class);
        intent.putExtra(INTENT_RESULT_CODE, code);
        intent.putExtra(TITLE, title);
        intent.putExtra(CONTENT, content);
        activity.startActivityForResult(intent, INTENT_REQUEST_CODE);
    }


}
